//最后点击的分类控件(select)
var lastCatSelect=null;
$(function(){
	lastCatSelect=$("#cat1")[0];
});
var arrayLevel=['一','二','三','四','五'];

//清空所有子类的显示
function clearChildCat(level){
	for(var lv=level;lv<=4;lv++){
		var cat=$("#cat"+lv)[0];
		fnRemoveAllOptions(cat);
		fnAddOption(cat,"","--"+arrayLevel[lv-1]+"级分类--");	
	}
}
function fillChildCategory(obj,level){
	
		//当前的分类级别
	var thisLevel=$(obj).attr("level");
	
	//若直接选择下一级分类，而没有选择父类，则需要提示
	clearChildCat(level);
	
	var fatherId=null;
	if(obj.value==''&& thisLevel >1){
		var fatherLevel=parseInt(thisLevel)-1;
		var fatherSelect=$("#cat"+fatherLevel)[0];
		var isSelected=false;
		for(var cn=0;cn<fatherSelect.options.length;cn++){
			if(fatherSelect.options[cn].selected&&
			fatherSelect.options[cn].value!=''){
				fatherId=fatherSelect.options[cn].value;
				isSelected=true;
				break;
			}
		}
		if(!isSelected){
			alert('请选择上一级分类～～');
			return;
		}
	}
	
	//设置显示当前的分类标签
	$("#spanLevel").html(arrayLevel[thisLevel-1]);
	//设置当前选择的分类select控件对象
	lastCatSelect=obj;
	
	//重置分类和子分类的添加表单
	$("#fatherForm")[0].reset();
	$("#showIcon").attr("src","");
	$("#childForm")[0].reset();
	$("#parentId").val(fatherId);
	
	//若分类>4，则返回，并且子类的添加界面关闭
	if(level>4||obj.value==''){
		$("#childForm").css('display','none');
	}else{
		$("#childForm").css('display','');
	}
	
	if(obj.value!=""){
		//填充该分类下的所有子类至下一级分类选择框
		categoryManager.getAllByParentId(parseInt(obj.value),{
			async:false,callback:function (data){
				//alert("the data length;"+data.length);
				var sourceObj=$("#cat"+level)[0];
				for(var i=0;i<data.length;i++){
					fnAddOption(sourceObj,data[i].categoryId,data[i].categoryName);
				}
				//加载当前选择的分类信息
				categoryManager.getById(parseInt(obj.value),function(data){
					$("#categoryName").val(data.categoryName);
					$("#sortOrder").val(data.sortOrder);
					var srcImg=$("#showIcon").attr("imgSrc");
					$("#showIcon").attr("src",srcImg+data.icon);
					$("#categoryId").val(data.categoryId);
					$("#parentId").val(data.fatherId);
					//设置其子类的父Id
					$("#_parentId").val(data.categoryId);
					if(data.status==1){
						$("#status").attr("checked",true);
					}else{
						$("#status").attr("checked",false);
						}
					});
			}

		});
		
		}
}
function callb(data,level){
	
}
/**
  当flag='_'时，则表示为子类添加或更新数据
**/
function saveOrUpdateCategory(flag){

	alert("#"+flag+"parentId");
	var categoryName=$("#"+flag+"categoryName").val();
	var sortOrder=$("#"+flag+"sortOrder").val();
	var categoryId=$("#"+flag+"categoryId").val();
	var parentId=$("#"+flag+"parentId").val();
	var ckStatus=$("#"+flag+"status")[0];
	var icon = '';
	var status=0;
	if(sortOrder==""){
		sortOrder=null;
	}
	if(ckStatus.checked){
		status=1;
	}
	if(categoryId==""){
		categoryId=null;
	}
	//if(parentId==""){
	//	parentId=null;
	//}
	if(parentId==null || parentId == 'null'){
		parentId='';
	}
	//-----------------------------
	//validate the data here [TODO]
	
	if(categoryName.trim()==""){
		alert("类别名称为必填！");
		$("#"+flag+"categoryName").focus();
		return;
	}
	if(categoryName.length>128){
		alert("类别名称不能超过64个字符！");
		$("#"+flag+"categoryName").focus();
		return;
	}
	var exPattern=/[0-9]*[1-9][0-9]*$/;
	if(!exPattern.test(sortOrder)){
		alert("输入的排序号不正确！");
		$("#"+flag+"sortOrder").focus();
		return;
	}
	
	//----------------------------
	var targetSelect=lastCatSelect;
	if(flag=='_'){
		var level=$(lastCatSelect).attr('level');
		nextLevel=parseInt(level)+1;
		targetSelect=$('#cat'+nextLevel)[0];
	}
	categoryManager.saveCategory(categoryId,categoryName,sortOrder,status,parentId,icon,
			function(data){
				alert('保存成功');
				//添加分类
				if(categoryId==null){
					targetSelect.options[targetSelect.options.length]=new Option(data.categoryName,data.categoryId);
					
				}else{//更新分类
					for(var i=0;i<targetSelect.options.length;i++){
						if(targetSelect.options[i].value==categoryId){
							targetSelect.options[i]=new Option(categoryName,categoryId);
							break;
						}
					}
				}
				$("#showIcon").attr("src","");
				//保留parentId以便下一次添加
				$("#parentId").val(parentId)
				$("#"+flag+"parentId").val(parentId)
				$("#childForm")[0].reset();
			});
}

function deleteCategory(){
	var categoryId=$("#categoryId").val();
	if(categoryId==null || categoryId=="")return ;
	//var lastCatSelect
	categoryManager.deleteById(categoryId,function(data){
		if (!data) {
			alert('不能删除所选分类，该分类下存在子分类或产品!');
			return;
		}
		
		for(var i=0;i<lastCatSelect.options.length;i++){
			if(lastCatSelect.options[i].value==categoryId){
				lastCatSelect.options[i]=null;
				break;
			}
		}
		
		var level=$(lastCatSelect).attr('level');
		level=parseInt(level)+1;
		$("#childForm").css('display','none');
		$("#fatherForm")[0].reset();
		if(level>4){
			alert("成功删除该分类!");
			return;	
		}
		clearChildCat(level)
		$("#showIcon").attr("src","");
		alert("成功删除该分类!");
	});
}